package com.jy.appmarket.screenlock.prefs;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 类说明：
 * 
 * @author jie.li
 * @date 2015-2-10
 * @version 1.0
 */
public class LockPrefs {
	private static SharedPreferences mInstance = null;
	
	public enum PasswordType{
		NO_PASSWORD(0),
		NUM_PASSWORD(1),
		PATTERN_PASSWORD(2);
		
		private int value;
		
		PasswordType(int value){
			this.value = value;
		}
		public int getType() {
			return value;
		}
	}
	
	public enum DelayLockTime{
		NOT(0,"立即"),
		FIVE_S(5,"5秒后"),
		FIFTH_S(15,"15秒后"),
		THIRTH_S(30,"30秒后"),
		ONE_M(60,"1分钟后"),
		FIVE_M(60*5,"5分钟后");
		
		private int value;
		private String text;
		
		DelayLockTime(int value, String text){
			this.value = value;
			this.text = text;
		}
		public int getValue() {
			return value;
		}
		public String getText(){
			return text;
		}
	}
	
	private static final String LOCK_SCREEN = "lock_screen";
	// 锁屏密码
	private static final String PASSWORD_PREFERENCE_KEY = "passcode";
	// 解锁密码类型
	private static final String PASSWORD_TYPE="password_type";
	// 是否开启锁屏
	private static final String START_LOCKER = "start_locker";
	// 是否开启锁屏声音
	private static final String LOCK_SOUND = "lock_sound";
	// 是否开启解锁按键触摸震动
	private static final String TOUCH_SOUND_FEEDBACK="touch_sound_feedback";
	// 延迟锁屏
	private static final String DELAY_LOCK_VALUE="delay_lock_value";
	private static final String DELAY_LOCK_TEXT = "delay_lock_text";
	// 是否是第一次进入”我的锁屏“
	private static final String FIRST_ENTER_MY_LOCK = "first_enter_my_lock";
	// 是否是第一次解锁
	private static final String FIRST_UNLOCK = "first_unlock";
	// 是否是插卡后第一次解锁
	private static final String FIRST_SIM_IN_UNLOCK = "first_sim_in_unlock";
	// 是否进行过锁屏初始化设置
	private static final String INIT_LOCK_SETTING = "init_lock_setting";
	// 是否显示锁屏new图标
	private static final String LOCK_NEW_FLAG = "lock_new_flag";
	
	private static SharedPreferences getInstance(Context ctx) {
		if (mInstance == null) {
			synchronized (LockPrefs.class) {
				if (mInstance == null) {
					mInstance = ctx.getSharedPreferences(LOCK_SCREEN,
							Context.MODE_PRIVATE);
				}
			}
		}
		return mInstance;
	}

	private static SharedPreferences get(Context ctx) {
		return getInstance(ctx);
	}

	public static void setNumberLockPassword(Context ctx, String password) {
		SharedPreferences sp = get(ctx);
		sp.edit().putString(PASSWORD_PREFERENCE_KEY, password).commit();
	}

	public static String getNumberLockPassword(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getString(PASSWORD_PREFERENCE_KEY, "");
	}

	/**
	 * 设置是否启动锁屏
	 * @param ctx
	 * @param start
	 */
	public static void setStartLocker(Context ctx, boolean start) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(START_LOCKER, start).commit();
	}

	public static boolean getStartLocker(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(START_LOCKER, false);
	}

	/**
	 * 设置是否锁屏播放声音
	 * @param ctx
	 * @return
	 */
	public static boolean isLockSoundOpen(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(LOCK_SOUND, true);
	}
	
	public static void setLockSoundOpen(Context ctx, boolean open) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(LOCK_SOUND, open).commit();
	}

	public static void setTouchSoundFeedback(Context ctx, boolean need) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(TOUCH_SOUND_FEEDBACK, need).commit();
	}

	public static boolean getTouchSoundFeedback(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(TOUCH_SOUND_FEEDBACK, true);
	}
	
	public static void setPasswordType(Context ctx, PasswordType type) {
		SharedPreferences sp = get(ctx);
		sp.edit().putInt(PASSWORD_TYPE, type.getType()).commit();
	}

	public static PasswordType getPasswordType(Context ctx) {
		SharedPreferences sp = get(ctx);
		int typeInt = sp.getInt(PASSWORD_TYPE, 0);
		if(typeInt == 1){
			return PasswordType.NUM_PASSWORD;
		}else if(typeInt == 2){
			return PasswordType.PATTERN_PASSWORD;
		}else{
			return PasswordType.NO_PASSWORD;
		}
	}
	
	public static void setDelayTime(Context ctx, DelayLockTime time) {
		SharedPreferences sp = get(ctx);
		sp.edit().putInt(DELAY_LOCK_VALUE, time.getValue()).commit();
		sp.edit().putString(DELAY_LOCK_TEXT, time.getText()).commit();
	}

	public static DelayLockTime getDelayTime(Context ctx) {
		SharedPreferences sp = get(ctx);
		int typeInt = sp.getInt(DELAY_LOCK_VALUE, 0);
		if(typeInt == 5){
			return DelayLockTime.FIVE_S;
		}else if(typeInt == 15){
			return DelayLockTime.FIFTH_S;
		}else if(typeInt == 30){
			return DelayLockTime.THIRTH_S;
		}else if(typeInt == 60){
			return DelayLockTime.ONE_M;
		}else if(typeInt == 60*5){
			return DelayLockTime.FIVE_M;
		}
		return DelayLockTime.NOT;
	}
	
	public static void setFirstEnterMyLock(Context ctx, boolean value) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(FIRST_ENTER_MY_LOCK, value).commit();
	}

	public static boolean isFirstEnterMyLock(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(FIRST_ENTER_MY_LOCK, true);
	}
	
	public static void setFirstUnlock(Context ctx, boolean value) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(FIRST_UNLOCK, value).commit();
	}

	public static boolean isFirstUnlock(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(FIRST_UNLOCK, true);
	}
	
	public static void setFirstSimInUnlock(Context ctx, boolean value) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(FIRST_SIM_IN_UNLOCK, value).commit();
	}

	public static boolean isFirstSimInUnlock(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(FIRST_SIM_IN_UNLOCK, true);
	}
	
	public static void setInitLockSetting(Context ctx, boolean value) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(INIT_LOCK_SETTING, value).commit();
	}

	public static boolean isInitLockSetting(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(INIT_LOCK_SETTING, false);
	}
	
	public static void setLockNewFlag(Context ctx, boolean value) {
		SharedPreferences sp = get(ctx);
		sp.edit().putBoolean(LOCK_NEW_FLAG, value).commit();
	}

	public static boolean isLockNewFlag(Context ctx) {
		SharedPreferences sp = get(ctx);
		return sp.getBoolean(LOCK_NEW_FLAG, true);
	}
}
